package com.teacherprofile.manage.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacherprofile.common.annotation.DataScope;
import com.teacherprofile.common.constant.AcademicPapersConstants;
import com.teacherprofile.common.constant.BusinessConstants;
import com.teacherprofile.common.core.domain.entity.SysUser;
import com.teacherprofile.common.enums.CheckStatus;
import com.teacherprofile.common.exception.ServiceException;
import com.teacherprofile.common.utils.DateUtils;
import com.teacherprofile.framework.manager.AsyncManager;
import com.teacherprofile.framework.manager.factory.AsyncFactory;
import com.teacherprofile.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.teacherprofile.manage.mapper.TechSpecialistMapper;
import com.teacherprofile.common.domain.TechSpecialist;
import com.teacherprofile.manage.service.ITechSpecialistService;
import org.springframework.util.CollectionUtils;

/**
 * 科技特派员Service业务层处理
 *
 * @author vibelin
 * @date 2025-05-17
 */
@Service
public class TechSpecialistServiceImpl extends ServiceImpl<TechSpecialistMapper, TechSpecialist> implements ITechSpecialistService {
    @Autowired
    private TechSpecialistMapper techSpecialistMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询科技特派员
     *
     * @param id 科技特派员主键
     * @return 科技特派员
     */
    @Override
    public TechSpecialist selectTechSpecialistById(Long id) {
        return techSpecialistMapper.selectTechSpecialistById(id);
    }

    /**
     * 查询科技特派员列表
     *
     * @param techSpecialist 科技特派员
     * @return 科技特派员
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TechSpecialist> selectTechSpecialistList(TechSpecialist techSpecialist) {
        return techSpecialistMapper.selectTechSpecialistList(techSpecialist);
    }

    /**
     * 新增科技特派员
     *
     * @param techSpecialist 科技特派员
     * @return 结果
     */
    @Override
    public boolean insertTechSpecialist(TechSpecialist techSpecialist) {
        String userName = techSpecialist.getUserName();
        if (ObjectUtils.isEmpty(userName)) {
            throw new RuntimeException("教职工号不能为空");
        }
        // 根据教职工号查询用户
        SysUser sysUser = userService.selectUserByUserName(userName);
        if (ObjectUtils.isEmpty(sysUser)) {
            // 用户不存在
            throw new RuntimeException("教职工号不存在");
        }
        // 用户存在，设置用户的userId与部门id，保存
        techSpecialist.setUserId(sysUser.getUserId());
        techSpecialist.setDeptId(sysUser.getDeptId());
        return save(techSpecialist);
    }

    /**
     * 修改科技特派员
     *
     * @param techSpecialist 科技特派员
     * @return 结果
     */
    @Override
    public boolean updateTechSpecialist(TechSpecialist techSpecialist) {
        return updateById(techSpecialist);
    }

    /**
     * 批量删除科技特派员
     *
     * @param ids 需要删除的科技特派员主键
     * @return 结果
     */
    @Override
    public int deleteTechSpecialistByIds(Long[] ids) {
        return techSpecialistMapper.deleteTechSpecialistByIds(ids);
    }

    /**
     * 删除科技特派员信息
     *
     * @param id 科技特派员主键
     * @return 结果
     */
    @Override
    public int deleteTechSpecialistById(Long id) {
        return techSpecialistMapper.deleteTechSpecialistById(id);
    }

    /**
     * 导入数据
     *
     * @param techSpecialist 工作荣誉
     * @return
     */
    @Override
    public String importData(List<TechSpecialist> techSpecialist) {
        if (CollectionUtils.isEmpty(techSpecialist)) {
            throw new ServiceException("导入数据不能为空！");
        }
        // 查询导入的教职工号去重集合
        Set<String> userNameList = techSpecialist.stream().map(TechSpecialist::getUserName).collect(Collectors.toSet());
        // 根据去重集合查询对应的用户
        List<SysUser> userList = userService.selectUserByUserNames(userNameList);
        if (ObjectUtils.isEmpty(userList)) {
            throw new ServiceException("导入数据中教职工号不存在！");
        }
        // 将列表转成Map，key为教职工号，value为SysUser对象
        Map<String, SysUser> userMap = userList.stream().collect(Collectors.toMap(SysUser::getUserName, item -> item));
        for (TechSpecialist item : techSpecialist) {
            // 填充用户的userId与deptId
            SysUser sysUser = userMap.get(item.getUserName());
            item.setUserId(sysUser.getUserId());
            item.setDeptId(sysUser.getDeptId());
        }
        return saveBatch(techSpecialist) ? "导入成功" : "导入失败";
    }

    /**
     * 审批
     *
     * @param techSpecialist 工作荣誉
     * @return
     */
    @Override
    public boolean check(TechSpecialist techSpecialist) {
        if (ObjectUtils.isNull(techSpecialist)) { // 非法参数
            throw new ServiceException(BusinessConstants.BUSINESS_ERROR);
        }
        if (techSpecialist.getCheckStatus().equals(CheckStatus.PASS.getCode())) {
            // 审核通过
            // TODO 发送mq消息，存储数据
            //rabbitMqHelper.send(MqConstants.Exchange.SOFTWARE_COPYWRITE_EXCHANGE,
            //                    MqConstants.Key.SOFTWARE_COPYWRITE_PASS_KEY, softwareCopyrights);

            // 通知用户，审核通过
            // TODO 修改常量
            AsyncManager.me().execute(AsyncFactory.sendPassMsgToOne(techSpecialist.getUserName(), AcademicPapersConstants.CHECKED_PASS));
        } else {
            // 审核不通过
            // TODO 发送mq消息，存储数据
            // 通知用户，审核不通过
            // TODO 修改常量
            AsyncManager.me().execute(AsyncFactory.sendRejectMsgToOne(techSpecialist.getUserName(), AcademicPapersConstants.CHECKED_REJECT));
        }
        // 删除此条数据

        return updateById(techSpecialist);
    }
}
